package cn.com.sbell.vss;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlContentHandler extends DefaultHandler implements ContentHandler {

	private String key = null;

	protected HashMap resultMap = new HashMap();

	public Map getResultMap() {
		return resultMap;
	}

	public void startElement(String namespace, String localname, String type,
			Attributes attributes) throws SAXException {
		key = type.trim();

		if (key.equalsIgnoreCase("result")) {
			String attrName = attributes.getQName(0);
			if (attrName.equalsIgnoreCase("code")) {
				resultMap.put("resultCode", attributes.getValue(attrName));
			}
		}
	}

	public void endElement(String namespace, String localname, String type)
			throws org.xml.sax.SAXException {
		key = null;
	}

	public void characters(char[] ch, int start, int len) {
		String text = new String(ch, start, len);
		String text1 = text.trim();
		if (text1.length() > 0) {
			if (key != null) {
				resultMap.put(key, text1);
				key = null;
			}
		}
	}

}
